package com.example.demo.controller;

import com.example.demo.dao.ClassMemberDao;
import com.example.demo.dao.ClassesDao;
import com.example.demo.entity.pojo.ClassMember;
import com.example.demo.entity.pojo.Classes;
import com.example.demo.entity.dto.TokenUserDto;
import com.example.demo.entity.vo.ResponseVO;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/class")
public class ClassesController extends BaseController {

    @Autowired
    private ClassesDao classesDao;

    @Autowired
    private ClassMemberDao classMemberDao;
    /**
     * 根据邀请码加入班级
     *
     * @param request HTTP请求对象，用于获取用户token
     * @param inviteCode 班级邀请码
     * @return 响应结果
     */
    @PostMapping("/joinClass")
    public ResponseVO<String> joinClass(HttpServletRequest request,
                                       @RequestParam String inviteCode) {
        try {
            // 验证用户登录状态
            TokenUserDto tokenUserDto = getTokenUserDto(request);
            if (tokenUserDto == null) {
                return getFail("登录超时，请重新登录！");
            }

            // 参数验证
            if (StringUtils.isBlank(inviteCode)) {
                return getFail("邀请码不能为空！");
            }

            // 根据邀请码查询班级
            Classes classes = classesDao.queryByInviteCode(inviteCode.trim());
            if (classes == null) {
                return getFail("邀请码无效，班级不存在！");
            }

            // 检查用户是否已经加入该班级
            ClassMember existingMember = classMemberDao.queryByClassIdAndUserId(
                classes.getId(), tokenUserDto.getUserId());
            if (existingMember != null) {
                return getFail("您已经是该班级的成员了！");
            }

            // 创建班级成员记录
            ClassMember classMember = new ClassMember();
            classMember.setClassId(classes.getId());
            classMember.setUserId(tokenUserDto.getUserId());
            classMember.setJoinTime(new Date());
            classMember.setStatus(1); // 1表示正常状态

            // 保存到数据库
            int result = classMemberDao.insert(classMember);
            if (result > 0) {
                return getSuccess("成功加入班级：" + classes.getClassName(), null);
            } else {
                return getFail("加入班级失败，请稍后重试！");
            }

        } catch (Exception e) {
            return getFail("系统错误：" + e.getMessage());
        }
    }

    /**
     * 获取用户已加入的班级列表
     *
     * @param request HTTP请求对象，用于获取用户token
     * @return 响应结果，包含用户已加入的班级列表
     */
    @GetMapping("/getMyClasses")
    public ResponseVO<List<Classes>> getMyClasses(HttpServletRequest request) {
        try {
            // 验证用户登录状态
            TokenUserDto tokenUserDto = getTokenUserDto(request);
            if (tokenUserDto == null) {
                return getFail("登录超时，请重新登录！");
            }

            // 查询用户加入的班级成员记录
            List<ClassMember> memberList = classMemberDao.queryByUserId(tokenUserDto.getUserId());

            // 根据班级ID查询班级详细信息
            List<Classes> myClassesList = new java.util.ArrayList<>();
            for (ClassMember member : memberList) {
                Classes classes = classesDao.queryById(member.getClassId());
                if (classes != null) {
                    myClassesList.add(classes);
                }
            }

            return getSuccess("获取我的班级列表成功！", myClassesList);

        } catch (Exception e) {
            return getFail("系统错误：" + e.getMessage());
        }
    }
}
